package com.ilxqx.framework.system.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ilxqx.framework.system.entity.anno.Excel;
import com.ilxqx.framework.system.enumeration.SysRoleDataOperationRangeEnum;
import com.ilxqx.framework.system.enumeration.SysRoleStatusEnum;
import lombok.Data;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * 系统角色实体类
 * @author venus
 */
@Entity(name = "sys_role")
@Data
@DynamicInsert
@DynamicUpdate
public class SysRole implements Serializable {

    private static final long serialVersionUID = 6611967803972880039L;

    /**
     * 角色ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long roleId;

    /**
     * 名称
     */
    @Excel(value = "名称")
    private String name;

    /**
     * 状态
     */
    @Excel(value = "状态", method = "getMeaning")
    private SysRoleStatusEnum status;

    /**
     * 数据操作范围
     */
    @Excel(value = "数据操作范围", method = "getMeaning")
    private SysRoleDataOperationRangeEnum dataOperationRange;

    /**
     * 备注
     */
    @Excel(value = "备注")
    private String remark;

    /**
     * 操作用户ID
     */
    private Long operationUserId;

    /**
     * 操作用户
     */
    @Excel(value = "操作用户")
    private String operationUserName;

    /**
     * 创建时间
     */
    @Excel(value = "创建时间")
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @Excel(value = "更新时间")
    private LocalDateTime updateTime;

    /**
     * 角色权限
     */
    @JsonIgnore
    @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
    @JoinTable(name = "SysRolePermission",
            joinColumns = @JoinColumn(name = "roleId"),
            inverseJoinColumns = @JoinColumn(name = "permissionId"))
    private Set<SysPermission> rolePermissions;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        SysRole sysRole = (SysRole) o;

        return new EqualsBuilder()
                .append(roleId, sysRole.roleId)
                .append(name, sysRole.name)
                .append(status, sysRole.status)
                .append(dataOperationRange, sysRole.dataOperationRange)
                .append(remark, sysRole.remark)
                .append(operationUserId, sysRole.operationUserId)
                .append(operationUserName, sysRole.operationUserName)
                .append(createTime, sysRole.createTime)
                .append(updateTime, sysRole.updateTime)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(roleId)
                .append(name)
                .append(status)
                .append(dataOperationRange)
                .append(remark)
                .append(operationUserId)
                .append(operationUserName)
                .append(createTime)
                .append(updateTime)
                .toHashCode();
    }

}